package com.yun.controller;

import com.yun.pojo.User;
import com.yun.service.UserService;
import com.yun.utils.GithubConstant;
import com.yun.utils.HttpClientUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Created with IDEA
 * @author: Super Zheng
 * @Description: 第三方登陆GitHub
 * @Date:2018/12/26
 * @Time:18:59
 */
@Controller
@RequestMapping("/api/github")
public class GithubLoginController {
    @Autowired
    private UserService userService;

    //回调地址
    @RequestMapping("/callback")
    public String callback(String code, String state, Model model, HttpServletRequest req) throws Exception {

        if (!StringUtils.isEmpty(code) && !StringUtils.isEmpty(state)) {
            //拿到我们的code,去请求token
            //发送一个请求到
            String token_url = GithubConstant.TOKEN_URL.replace("CODE", code);
            //得到的responseStr是一个字符串需要将它解析放到map中
            String responseStr = HttpClientUtils.doGet(token_url);
            // 调用方法从map中获得返回的--》 令牌
            String token = HttpClientUtils.getMap(responseStr).get("access_token");
            //根据token发送请求获取登录人的信息  ，通过令牌去获得用户信息
            String userinfo_url = GithubConstant.USER_INFO_URL.replace("TOKEN", token);
            responseStr = HttpClientUtils.doGet(userinfo_url);//json

            Map<String, String> responseMap = HttpClientUtils.getMapByJson(responseStr);
            // 成功则登陆
            System.out.println("-------------333------------");
            Set<String> s = responseMap.keySet();
            Collection<String> c = responseMap.values();
            Iterator<String> it2 = s.iterator();
            Iterator<String> it3 = c.iterator();
            while (it3.hasNext()) {
                String str = it2.next();
                String str1 = it3.next();
                System.out.println(str + "---" + str1);
            }
            String username = "github_" + responseMap.get("login");
            String password = responseMap.get("node_id").substring(0, 13);
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            System.out.println("gitctrl:" + user);
            req.getSession().setAttribute("githubUser", user);
            return "redirect:/user/githubRegister.action";

//            return "/main";
        }
        // 否则返回到登陆页面
        return "redirect:/user/goLogin.action";
    }

    @RequestMapping("/githubLogin")
    public String githubLogin() {
        String state = RandomStringUtils.random(24, "abcdefghijklmnopqrstuvwxyz0123456789");
        String url = "https://github.com/login/oauth/authorize?client_id=1f04e2961c3b9320ba4e&state=" + state + "&redirect_uri=http://localhost:8080/SharedCloud_war_exploded/api/github/callback.action";
        return "redirect:" + url;
    }

}